package com.ym.soso.service.impl;

import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.soso.dao.OrgCommentRecordDao;
import com.ym.soso.domain.entity.OrgCommentRecordEntity;
import com.ym.soso.domain.entity.TagCommontMiddleEntity;
import com.ym.soso.domain.param.AuditParam;
import com.ym.soso.domain.param.CountLabelParam;
import com.ym.soso.domain.param.OrgCommentRecordQueryParam;
import com.ym.soso.domain.param.OrgCommentRecordUpdateParam;
import com.ym.soso.domain.param.UserEvaluationParam;
import com.ym.soso.domain.vo.CountLabelVo;
import com.ym.soso.domain.vo.OrgCommentRecordListPcVO;
import com.ym.soso.domain.vo.OrgCommentRecordVO;
import com.ym.soso.enums.AuditStateEnum;
import com.ym.soso.service.IOrgCommentRecordSV;
import com.ym.soso.service.ITagCommontMiddleSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 评价记录Service业务层处理
 *
 * @author ym
 * @date 2020-08-20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrgCommentRecordSVImpl extends BaseSVImpl<OrgCommentRecordEntity, OrgCommentRecordDao> implements IOrgCommentRecordSV {

    /**
     * 查询评价记录列表
     *
     * @param queryParam 评价记录 查询参数
     * @return 评价记录
     */
    @Override
    public List<OrgCommentRecordListPcVO> selectListPC(OrgCommentRecordQueryParam queryParam) {
        List<OrgCommentRecordListPcVO> orgCommentRecords = mapper.selectListPC(queryParam);
        return orgCommentRecords;
    }

    /**
     * 新增评价记录
     *
     * @param updateParam 评价记录 新增参数
     * @return 结果
     */
    @Override
    public Long insertPC(OrgCommentRecordUpdateParam updateParam) {
        OrgCommentRecordEntity entity = updateParam.covert();
        this.insert(entity);
        return entity.getId();
    }

    /**
     * 修改评价记录
     *
     * @param updateParam 评价记录 修改参数
     * @return 结果
     */
    @Override
    public int updateByIdPC(OrgCommentRecordUpdateParam updateParam) {
        OrgCommentRecordEntity entity = updateParam.covert();
        int result = this.updateById(entity);
        return result;
    }

    @Override
    public int audit(AuditParam auditParam) {
        return updateByParams(
                OrgCommentRecordEntity
                        .builder()
                        .build(),
                TK.selectWhere(OrgCommentRecordEntity.class)
                        .andIn(OrgCommentRecordEntity::getId, auditParam.getIds()).end()
        );
    }

    @Autowired
    private ITagCommontMiddleSV tagCommontMiddleSV;

    @Autowired
    private OrgCommentRecordDao orgCommentRecordDao;

    /**
     * 用户评价录入
     *
     * @param updateParam
     * @return
     */
    @Override
    public Long evalInsert(UserEvaluationParam updateParam) {
        //1. 保存评价记录
        OrgCommentRecordEntity orgCommentRecordEntity = updateParam.covert("tagIds");

        //默认评价通过
        orgCommentRecordEntity.setComontSb(ShiroUtil.getLoginId());
        orgCommentRecordEntity.setAuditState(AuditStateEnum.AUDIT.getKey());
        orgCommentRecordEntity.setAuditTime(new Date());
        Long id = insert(orgCommentRecordEntity);

        //2.关联标签和评价
        if (updateParam.getTagIds() != null && updateParam.getTagIds().length > 0) {
            for (Long tagId : updateParam.getTagIds()) {
                tagCommontMiddleSV.insert(
                        TagCommontMiddleEntity
                                .builder()
                                .tagId(tagId)
                                .commontId(id)
                                .build()
                );
            }
        }

        return id;
    }

    /**
     * 评价记录列表
     * @param queryParam
     * @return
     */
    @Override
    public List<OrgCommentRecordVO> selectListMobile(OrgCommentRecordQueryParam queryParam) {
        return  orgCommentRecordDao.selectListMobile(queryParam);
    }



    /**
     * 评价统计
     * @param queryParam
     * @return
     */
    @Override
    public CountLabelVo countLabel(CountLabelParam queryParam) {
        CountLabelVo countLabelVo = new CountLabelVo();
        //1. 评价标签统计
        countLabelVo.setTaglibraryVO(orgCommentRecordDao.countLabel(queryParam));

        //2. 评价记录总数
        countLabelVo.setCountNum(
                selectListMobile(
                        OrgCommentRecordQueryParam
                                .builder()
                                .relId(queryParam.getRelId())
                                .relType(queryParam.getRelType())
                                .tglbName(queryParam.getTglbName())
                                .build()
                ).size()
        );
        return countLabelVo;
    }
}
